<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>高校宿舍管理系统-我的宿舍</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="#"/>
    <link rel="stylesheet" href="../layui/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../layui/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- 表格头部工具栏 -->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" id="chBed" lay-event="chBed">选择床位</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <!-- 添加和修改窗口 -->
        <div style="display: none;padding: 5px" id="chooseBedWindow">
            <form class="layui-form" id="dataFrm" lay-filter="dataFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">宿舍布局</label>
                    <div class="layui-input-block">
                        <img src="../images/dorm.jpg" alt="宿舍布局图" height="300px" width="400px">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选择床位</label>
                    <div class="layui-input-block" style="margin-left: 130px">
                        <input type="radio" name="bed" value="1" title="1号床">
                        <input type="radio" name="bed" value="2" title="2号床">
                        <input type="radio" name="bed" value="3" title="3号床">
                        <input type="radio" name="bed" value="4" title="4号床">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="text-align: center;margin-right: 110px;margin-top: 10px;">
                        <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit">
                            <span class="layui-icon layui-icon-add-1"></span>提交
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="../layui/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            table = layui.table;

        var url;
        var windowIndex;//窗口的索引
        var chooseStatus = 0;// 是否已经选择床位了

        var tableIns = table.render({
            elem: '#currentTableId',
            url: '/student/mydorm/list',
            toolbar: '#toolbarDemo',
            cols: [
                [
                    {field: 'brcode', title: "宿舍编号", hide: true},
                    {field: 'brarea', width: 150, title: '宿舍区', align: "center"},
                    {field: 'brbid', width: 80, title: '楼栋', align: "center"},
                    {field: 'brrid', width: 100, title: '寝室', align: "center"},
                    {field: 'bed', width: 100, title: '床位号', align: "center"},
                    {field: 'stuname', width: 150, title: '姓名'},
                    {field: 'stuid', width: 160, title: '学号', align: "center"},
                    {field: 'stucollege', minWidth: 150, title: '学院', align: "center"},
                    {field: 'studept', title: '专业', width: 130, align: "center"},
                    {field: 'stuclass', width: 100, title: '班级', align: "center"},
                    {field: 'stutel', width: 150, title: '联系方式', align: "center"}
                ]
            ],
            limits: [4, 6, 8, 12, 16],
            limit: 6,
            page: true,
            skin: 'line',
            even: true
        });

        $(function () {
            getChooseStatus();
        });

        $("#chBed").on("click", function () {
            if (chooseStatus == 1) {
                layer.msg("您已选择床位，不可再次选择！", {icon: 2});
            } else {
                openChooseWindow();
            }
        });

        /**
         * 判断是否已经选择了床位
         */
        function getChooseStatus() {
            $.ajax({
                url: "/student/mydorm/isChoosed",
                type: "get",
                dataType: "json",
                success: function (result) {
                    if (!result.status) {
                        layer.confirm("床位还未选择，请先选择床位！", {icon: 0, title: '提示'}, function (index) {
                            openChooseWindow();
                            layer.close(index);
                        });
                    } else {
                        chooseStatus = 1;
                    }
                },
                error: function () {
                    layer.msg("网络错误，请稍后再试！", {icon: 5});
                    return false;
                }
            });
        }

        // 打开选择床位的窗口
        function openChooseWindow() {
            windowIndex = layer.open({
                type: 1,
                title: "选择床位",
                area: ["600px", "500px"],
                content: $("#chooseBedWindow"),
                success: function () {
                    $("#dataFrm")[0].reset();
                    // 查找已经选了的床位，设置对应的单选按钮为不可选状态
                    $.ajax({
                        url: "/student/mydorm/alreadyBeds",
                        type: "get",
                        dataType: "json",
                        success: function (result) {
                            var beds = result.beds;
                            beds.forEach(function (item) {
                                // 设置对应的单选按钮为不可选状态
                                $(':radio[value=' + item + ']').prop('disabled', 'disabled');
                                $(':radio[value=' + item + ']').next().addClass('layui-radio-disbaled layui-disabled');
                            })
                        }
                    });
                    url = "/student/mydorm/chooseBed";
                }
            });
        }

        // 监听选床位窗口中表单提交事件
        form.on("submit(doSubmit)", function (data) {
            var bed = data.field.bed;
            if (bed == null || bed == '') {
                layer.msg("请选择床位号！", {icon: 0});
                return false;
            }
            $.post(url, {"bed": bed}, function (result) {
                if (result.success) {
                    tableIns.reload();
                    layer.close(windowIndex);
                    chooseStatus = 1;
                    layer.msg(result.msg, {icon: 1});
                } else {
                    layer.msg(result.msg, {icon: 5});
                }
            }, "json");
            return false;
        });
    });
</script>

</body>
</html>